package bb.lanxing.activity.mine;

import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.widget.Button;
import android.widget.EditText;

import com.baidu.mapapi.model.LatLng;

import java.text.MessageFormat;

import bb.lanxing.App;
import bb.lanxing.R;
import bb.lanxing.activity.base.BaseActivity;
import bb.lanxing.manager.SharedManager;
import bb.lanxing.util.BiCiCoorConverter;

public class SosActivity extends BaseActivity {
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.help_sos);
        setupActionBar(true);
        final EditText editText = findViewById(R.id.sosContent);
        if (editText != null) {
            LatLng curLatLngWithMP = SharedManager.getInstance().getCurLatLngWithMP();
            if (curLatLngWithMP == null) {
                App.getContext().showMessage(R.string.mine_setting_sos_toast_get_location_failed);
            } else {
                LatLng earth2Common = BiCiCoorConverter.earth2Common(curLatLngWithMP);
                editText.setText(getString(R.string.mine_setting_sos_content_send, MessageFormat.format("{0,number,#.######},{1,number,#.######}", earth2Common.latitude, earth2Common.longitude)));
            }
        }
        Button button = findViewById(R.id.sendSOSBySms);
        if (button != null) {
            button.setOnClickListener(view -> {
                Intent intent = new Intent("android.intent.action.VIEW");
                intent.setData(Uri.parse("sms:"));
                intent.putExtra("sms_body", editText.getText().toString());
                SosActivity.this.startActivity(intent);
            });
        }
        Button button2 = findViewById(R.id.sendSOSByOthers);
        if (button2 != null) {
            button2.setOnClickListener(view -> {
                Intent intent = new Intent("android.intent.action.SEND");
                intent.setType("text/plain");
                intent.putExtra("android.intent.extra.SUBJECT", getString(R.string.mine_setting_sos_dialog_subject));
                intent.putExtra("android.intent.extra.TEXT", editText.getText().toString());
                intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
                SosActivity sosActivity = SosActivity.this;
                sosActivity.startActivity(Intent.createChooser(intent, getString(R.string.mine_setting_sos_dialog_title)));
            });
        }
    }
}
